package org.example.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.demo.constant.MessageConstant;
import org.example.demo.entity.InformationHint;
import org.example.demo.service.InformationHintService;
import org.example.demo.util.AjaxResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 信息提示控制器
 */
@RestController
@RequestMapping("informationHint")
public class InformationHintController {

    @Resource
    private InformationHintService informationHintService;

    /**
     * 分页查询信息提示
     * 
     * @param informationHint 查询条件
     * @param pageNum         页码
     * @param pageSize        每页大小
     * @return 查询结果
     */
    @PostMapping("page")
    public AjaxResult page(@RequestBody InformationHint informationHint,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<InformationHint> page = informationHintService.selectPage(informationHint, pageNum, pageSize);

        Map<String, Object> result = new HashMap<>();
        result.put("page", page);
        result.put("total", page.getTotal());

        return AjaxResult.success(result);
    }

    /**
     * 根据ID查询信息提示
     * 
     * @param id 信息提示ID
     * @return 查询结果
     */
    @GetMapping("{id}")
    public AjaxResult getById(@PathVariable Long id) {
        InformationHint informationHint = informationHintService.getById(id);

        if (informationHint != null) {
            return AjaxResult.success(informationHint);
        } else {
            return AjaxResult.error("信息提示不存在");
        }
    }

    /**
     * 新增信息提示
     * 
     * @param informationHint 信息提示信息
     * @return 操作结果
     */
    @PostMapping
    public AjaxResult add(@RequestBody InformationHint informationHint) {
        if (informationHintService.save(informationHint)) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error(MessageConstant.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 修改信息提示
     * 
     * @param informationHint 信息提示信息
     * @return 操作结果
     */
    @PutMapping
    public AjaxResult update(@RequestBody InformationHint informationHint) {
        if (informationHintService.updateById(informationHint)) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error(MessageConstant.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 删除信息提示
     * 
     * @param id 信息提示ID
     * @return 操作结果
     */
    @DeleteMapping("{id}")
    public AjaxResult delete(@PathVariable Long id) {
        if (informationHintService.removeById(id)) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error(MessageConstant.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 根据用户ID查询信息提示列表
     *
     * @param userId   用户ID
     * @param pageNum  页码
     * @param pageSize 每页大小
     * @return 查询结果
     */
    @GetMapping("user/{userId}")
    public AjaxResult getByUserId(@PathVariable Long userId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<InformationHint> page = informationHintService.getByUserId(userId, pageNum, pageSize);

        Map<String, Object> result = new HashMap<>();
        result.put("page", page);
        result.put("total", page.getTotal());

        return AjaxResult.success(result);
    }
}